/*
Copyright (c) 2025 Huawei Technologies Co., Ltd.
This file is a part of the CANN Open Software.
Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
Please refer to the License for details. You may not use this file except in compliance with the License.
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE in the root of the software repository for the full text of the License. 
*/

#pragma once
#include "kernel_operator.h"

/**
算子内部用到的utils函数
*/


/**
 * @brief val 向上按照 align 的倍数取整
 * @param [in] T val：需要取整的数
 * @param [in] align：需要向上取整的最小粒度
 * @return T 向上对齐后的值
 */
template<typename T>
[aicore] inline __attribute__((always_inline)) T RoundUp (const T val, const T align) {
    if ( align == 0 ) {
        return 0;
    } else {
        return ( val + align - 1 ) / align * align ;
    }
}



/**
 * @brief val 向下按照 align 的倍数取整
 * @param [in] T val：需要取整的数
 * @param [in] T align：需要向下取整的最小粒度
 * @return T 向下对齐后的数
 */
template<typename T>
[aicore] inline __attribute__((always_inline)) T RoundDown ( const T val, const T align ) {
    if ( align == 0 ) {
        return 0;
    } else {
        return val / align * align;
    }
}

/**
 * @brief val 除以 divisor 向上取整 
 * @param [in] T val：被除数
 * @param [in] T divisor：除数
 * @return T val除以divisor向上取整的结果
 */
template<typename T>
[aicore] inline __attribute__((always_inline)) T CeilDiv ( const T val, const T divisor ){
    if (  divisor == 0 ) {
        return 0;
    } else {
        return ( val + divisor - 1 ) / divisor;
    }

}
